package cn.chrisx.drad.audiencenetwork;

import android.app.Activity;
import android.content.Context;
import android.view.ViewGroup;

import com.facebook.ads.AdSize;
import com.facebook.ads.AdView;

/**
 * @author Jzfox <jz_boy@163.com>
 * @version 1.0
 * @date 2018/9/26 15:21
 * @description
 * @copyright ©2018 www.jzfox.net All rights reserved.
 */
public class BannerImpl extends BaseAd {

    private AdView adView;


    @Override
    public boolean loadAd(Context context) {


        // Instantiate an AdView object.
        // NOTE: the placement ID will eventually identify this as your App, you can ignore it for
        // now, while you are testing and replace it later when you have signed up.
        // While you are using this temporary code you will only get test ads and if you release
        // your code like this to the Google Play your users will not receive ads (you will get a no fill error).

        if (adView == null) {

            AdSize adSize = AdSize.BANNER_320_50;

            if (drAdConfig instanceof AudienceNetworkAdConfig) {
                adSize = ((AudienceNetworkAdConfig) drAdConfig).getAdSize();
            }

            adView = new AdView(context, drAdConfig.getAdUnitId(), adSize);
            adView.setAdListener(this);
        }

        adView.loadAd();
        return true;
    }


    @Override
    public void onDestroy(Context context) {
        if (adView != null) adView.destroy();
    }

    @Override
    public boolean show() {

        ViewGroup root = drAdConfig.getAdContainer();

        if (isValid() && root != null) {

            root.removeAllViews();
            root.addView(adView);

            return true;
        }

        return false;
    }

    @Override
    public boolean isValid() {
        return adView != null && !adView.isAdInvalidated();
    }
}
